/*
    This file is part of Raptor.

    Raptor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Raptor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Raptor.  If not, see <http://www.gnu.org/licenses/>.

 */

#ifndef URLCONTAINER_H
#define URLCONTAINER_H

#include "../../../include/interfaceUrlContainer.h"

#include <QtGui>
#include <QObject>

class pluginUrlContainer : public QObject, InterfaceUrlContainer
{
    Q_OBJECT
    Q_INTERFACES(InterfaceUrlContainer)

    public:

        pluginUrlContainer();

        virtual void start(QUrl const& url, QString const& captchaDir);
        virtual void abort();

        virtual void setProxy(QNetworkProxy const& proxy);
        virtual void setCaptcha(QString const& captcha);
        virtual bool isValidUrl(QUrl const& url);

    signals:

        void finished(QStringList const& urlList, bool const& error);
        void captcha(QString const& file);
        void stateChanged();
        void progressChanged(int const& progress);

    private slots:

        void slot_http_requestFinished(int id, bool error);
        void slot_http_readyRead(const QHttpResponseHeader & resp);
        void slot_http_dataReadProgress(int done, int total);
        void slot_http_sslErrors(const QList<QSslError> & errors);

    private:

        QString page;
        QByteArray response;
        QByteArray cookie;
        int waitingId;
        int step;
        int counter;

        QStringList links;
        QStringList maskedLinks;
        QStringList framedLinks;

        QRegExp reHost;
        QRegExp reLink;

        QRegExp reCaptcha;
        QRegExp reCaptchaFile;
        QRegExp reIp;
        QRegExp reGetFile;
        QRegExp reFrame;

        QString imageName;
        QUrl _url;

        void finish(bool const& error);
        void loadCaptcha();
        int request(QUrl const& url, QString const& method, QByteArray const& post, QMap<QString, QString> const& headerData, QIODevice *io=0);
        QString fromUnicode(QString const& str);
};

#endif // URLCONTAINER_H
